package com.sf.ams.json;

import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;


/**
 * 描述：
 * 
 * <pre>HISTORY
 * ****************************************************************************
 *  ID   DATE           PERSON          REASON
 *  1    2017年10月20日      Simba.Hua         Create
 * ****************************************************************************
 * </pre>
 * @author Simba.Hua
 */
public class JsonFilterSerializer {
	private static final String DYNC_INCLUDE = "DYNC_INCLUDE";//包含的标识
	private static final String DYNC_EXCLUDE = "DYNC_EXCLUDE";//过滤的标识
	private ObjectMapper mapper = new ObjectMapper();
	@JsonFilter(DYNC_EXCLUDE)
	interface DynamicExclude{
		
	}
	@JsonFilter(DYNC_INCLUDE)
	interface DynamicInclude{
		
	}
	public void filter(Class<?> clazz , String include , String exclude) {
		if (clazz == null) return;
		/*mapper = new ObjectMapper();
		List<String> excludes =  Arrays.asList(exclude.split(","));
		StringJoiner newExcludes = new StringJoiner(",");
		StringJoiner subExcludes = new StringJoiner(",");
		excludes.forEach(e -> {
			if(e.indexOf('.') != -1){
				subExcludes.add(e);
			} else {
				newExcludes.add(e);
			}
		});
		if(subExcludes.length()>0){
			StringTokenizer st = new StringTokenizer(subExcludes.toString(),",");
			while(st.hasMoreTokens()){
				String field = st.nextToken();
				List<String> fields = Arrays.asList(field.split("\\."));
				Class<?> subClass = getSubClass(clazz,fields.get(0));
				filter(subClass, null , fields.get(1));
			}
		}*/
		if (include != null && include.length() > 0) {//包含的操作
			mapper.setFilterProvider(new SimpleFilterProvider()
			.addFilter(DYNC_INCLUDE, SimpleBeanPropertyFilter.filterOutAllExcept(include.split(","))));//多个字段用,分割开
			mapper.addMixIn(clazz, DynamicInclude.class);
		} else if (exclude != null && exclude.length() > 0) {
			mapper.setFilterProvider(new SimpleFilterProvider()
			.addFilter(DYNC_EXCLUDE, SimpleBeanPropertyFilter.serializeAllExcept(exclude.split(","))));
			mapper.addMixIn(clazz, DynamicExclude.class);
		}
		
	}
	public String toJson(Object object) throws JsonProcessingException{
		return mapper.writeValueAsString(object);
	}
	
	/*private  Class<?> getSubClass(Class<?> parentClazz,String subField){
		Field declaredField = null;
		try {
			declaredField = parentClazz.getDeclaredField(subField);
			ParameterizedType pt = (ParameterizedType)declaredField.getGenericType();
			return (Class<?>) pt.getActualTypeArguments()[0];
		} catch (NoSuchFieldException | SecurityException e) {
			e.printStackTrace();
		}
		return null;
	}*/
	/*@Test
	public void testFilterJson() throws JsonProcessingException{
		JsonFilterSerializer jsonFilter = new JsonFilterSerializer();
		jsonFilter.filter(SystemInfo.class, null, "systemCode,environments.dataBasePassword");//Response中有status、failReason、currentIndex三个属性
		SystemInfo system = new SystemInfo();
		system.setSystemCode("RPS");
		List<Environment> environments = new ArrayList<>();
		environments.add(new Environment( "SIT", "http://10.0.22.32",
				"password123"));
		environments.add(new Environment( "UAT", "http://10.0.22.32",
				"password456"));
		system.setEnvironments(environments);
		System.out.println(jsonFilter.toJson(system));
	}
	@Test
	public void testSubClass() throws NoSuchFieldException, SecurityException{
		Class<?> clazz = SystemInfo.class;
		Field declaredField = clazz.getDeclaredField("environments");
		ParameterizedType pt = (ParameterizedType)declaredField.getGenericType();
		
		System.out.println(pt.getActualTypeArguments()[0]);
		
//		Type[] actualTypeArguments = ((ParameterizedType)declaredField.getType().getGenericSuperclass()).getActualTypeArguments();
//		System.out.println(actualTypeArguments[0]);
	}*/
}
